package controllers

import (
	"fmt"
	"github.com/astaxie/beego"
	"github.com/tealeg/xlsx"
	"meican/models"
	"meican/util"
	"strconv"
	"strings"
	"time"
)

type OrdertmpController struct {
	baseController
}

func (this *OrdertmpController) Index() {
	var result []map[string]string
	var totalPrice float64 = 0
	var total int64 = 0
	//获取今天开始时间
	timeStr := time.Now().Format("2006-01-02")
	t, _ := time.Parse("2006-01-02", timeStr)
	timeNumber := t.Unix()
	//nowtime := time.Now().Unix()//获取当前时间

	query := new(models.Ordertmp).Query().Filter("order_time", timeNumber)
	count, _ := query.Count()
	if count > 0 {
		var list []*models.Ordertmp
		var price int = 0

		query.All(&list)
		for _, val := range list {
			var order = make(map[string]string)
			price += val.Price
			total += 1
			order["userName"] = val.UserName
			order["menuName"] = val.MenuName
			order["price"] = strconv.Itoa(val.Price)
			orderTime := time.Unix(int64(val.OrderTime), 0)
			//p, _ := time.Parse("2006-01-02 15:04:05", orderTime.Unix())
			order["orderTime"] = orderTime.Format("2006-01-02")
			result = append(result, order)
		}
		totalPrice = float64(price)
	}
	if this.IsAjax() {
		this.Data["json"] = map[string]interface{}{"status": 1, "total": total, "totalPrice": totalPrice, "list": &result}
		this.ServeJSON()
	} else {
		this.Data["webHost"] = beego.AppConfig.String("WebHost")
		this.Data["todayDate"] = time.Now().Format("01-02")
		this.Data["totalPrice"] = totalPrice
		this.Data["total"] = total
		this.Data["list"] = result
		this.Display()
	}

}

//提交订单
func (this *OrdertmpController) Order() {
	ordertmp := new(models.Ordertmp)
	if !this.IsPost() {
		this.Rsp(0, "非法请求")
	}
	userName := strings.TrimSpace(this.GetString("user_name"))
	menuName := strings.TrimSpace(this.GetString("menu_name"))
	price, _ := this.GetInt("price")
	fmt.Println(userName)
	ordertmp.UserName = userName
	ordertmp.MenuName = menuName
	ordertmp.Price = price

	//获取今天开始时间
	timeStr := time.Now().Format("2006-01-02")
	t, _ := time.Parse("2006-01-02", timeStr)
	timeNumber := t.Unix()
	ordertmp.OrderTime = int(timeNumber)
	if err := ordertmp.Insert(); err != nil {
		//this.Rsp(0, err.Error())
		this.Rsp(0, "下单失败")
	}

	this.Rsp(1, "下单成功")
}

//导出订单
func (this *OrdertmpController) Export() {
	var list []*models.Ordertmp
	//获取今天开始时间
	timeStr := time.Now().Format("2006-01-02")
	t, _ := time.Parse("2006-01-02", timeStr)
	timeNumber := t.Unix()

	query := new(models.Ordertmp).Query().Filter("order_time", timeNumber)
	query.All(&list)

	file := xlsx.NewFile()
	sheet, err := file.AddSheet("Sheet1")
	if err != nil {
		panic(err)
	}
	title := sheet.AddRow()
	title.SetHeightCM(0.8)
	tcell := title.AddCell()
	tcell.Value = "序号"
	tcell = title.AddCell()
	tcell.Value = "姓名"
	tcell = title.AddCell()
	tcell.Value = "菜名"
	tcell = title.AddCell()
	tcell.Value = "价格"
	tcell = title.AddCell()
	tcell.Value = "下单日期"
	for key, val := range list {
		row := sheet.AddRow()
		row.SetHeightCM(0.6)
		cell := row.AddCell()
		cell.Value = strconv.Itoa(key + 1)
		cell = row.AddCell()
		cell.Value = val.UserName
		cell = row.AddCell()
		cell.Value = val.MenuName
		cell = row.AddCell()
		cell.Value = strconv.Itoa(val.Price)
		cell = row.AddCell()
		cell.Value = util.IntToDate(val.OrderTime)
	}
	var fileName = "data/order.xlsx"
	file.Save(fileName)
	this.DownloadFile(fileName, timeStr+"-订单.xlsx")
}
